<?php

namespace app\common\logic;

use think\Model;

class AuthGroup extends Model
{
    protected $autoWriteTimestamp = true;
    protected $insert = ['operator'];

    protected function setOperatorAttr($value)
    {
        return getUid();
    }

    protected function setRulesAttr($value)
    {
        if (!empty($value))
            return ',' . implode(',', $value) . ',';

        return '';
    }

    protected function getRulesAttr($value)
    {
        if (!empty($value))
            return array_values(array_filter(explode(',', $value)));

        return [];
    }

    protected function getCreateTimeAttr($value)
    {
        return recent_time($value);
    }

    protected function getUpdateTimeAttr($value)
    {
        return recent_time($value);
    }

    public function addItem($data)
    {
        $ret = $this->allowField(true)->isUpdate(false)->save($data);
        return $ret;
    }

    public function updateItem($data, $where)
    {
        $ret = $this->allowField(true)->isUpdate(true)->save($data, $where);
        return $ret;
    }

    public static function getList($where, $field = "*", $order = "create_time")
    {
        $rows = empty(input('rows')) ? config('paginate.list_rows') : input('rows');
        $list = self::where($where)->order($order)->field($field)->paginate($rows);
        $ret = $list->render();
        return $ret;
    }
}